package zcj.stu.service;

import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import zcj.stu.Entity.Category;
import zcj.stu.dao.CategoryDao;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CategoryService {
    @Resource
    private CategoryDao categoryDao;
    public boolean add(Category category) {
        return categoryDao.add(category)!=null;
    }

    public boolean edit(Category category) {
        Query query = Query.query(Criteria.where("_id").is(category.get_id()));
        Update update = new Update();
        update.set("name",category.getName());
        update.set("isShow",category.getIsShow());
        return categoryDao.edit(query,update).getModifiedCount()>0;
    }

    public boolean del(String id) {
        Query query = Query.query(Criteria.where("_id").is(id));
        return categoryDao.del(query).getDeletedCount()>0;
    }

    public List<Category> all() {
        return categoryDao.all();
    }
}
